package com.ocom.accesservice.controller;


import com.ocom.accesservice.feign.MqSimpleFeign;
import com.ocom.accesservice.service.AttendanceRulesService;
import com.ocom.accesservice.service.ZoneDateService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityAttendanceRules;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
public class TimerController {

    @Autowired
    AttendanceRulesService attendanceRulesService;

    @Autowired
    ZoneDateService zoneDateService;

    @Autowired
    MqSimpleFeign mqSimpleFeign;

    //定时任务
    @GetMapping(AccessServiceUrl.Url_attendanceRulesTimer)
    public Result<List<EntityAttendanceRules>> attendanceRuleTimer() {
        List<EntityAttendanceRules> list = attendanceRulesService.getTaskList(100l, 1);
        handle(list);
        list = attendanceRulesService.getTaskList(100l, 2);
        handle(list);
        list = attendanceRulesService.getTaskList(600l, 1);
        handle(list);
        list = attendanceRulesService.getTaskList(600l, 2);
        handle(list);
        return ResultUtil.success();
    }

    void handle(List<EntityAttendanceRules> list) {
        for (EntityAttendanceRules entityAttendanceRules : list) {
            Long zoneNo = zoneDateService.getZoneNo(entityAttendanceRules.getComId());
            if(zoneNo.equals(entityAttendanceRules.zoneNo.longValue())){
                mqSimpleFeign.attendance(entityAttendanceRules);
            }
        }
    }


}
